// pages/schoolInfo/schoolInfo.js
import { school } from "../../server/school";
import { file } from "../../server/file";


Page({

    /**
     * 页面的初始数据
     */
    data: {
        schoolInfo: {
            id: '',
            name: "",
            city: "",
            tag: "",
            isCollect: false,
            introduction: "",
            email: "",
            positionList: [],
            logoUrl: ''
        },
        majorList: [],          // 所有的专业
        showMajorList: [],      // 显示的前3个专业
        zixunList: [],          // 资讯列表
        zixunListLen: 0,        // 资讯数量
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 存储当前页面的院校ID
        const schoolId = options.schoolId;
        let schoolInfo = this.data.schoolInfo;
        schoolInfo.id = schoolId;
        this.setData({ schoolInfo });
        console.log('学校ID:', options);
        // 获取学校基本信息
        this.getSchoolInfo(schoolId);
        // 获取学校专业
        this.getAllMajor(schoolId);
        // 获取所有资讯
        this.getAllzixun(schoolId);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    
    /*
     * 返回上一页
     */
    returnMainPage() {
        wx.navigateBack({ delta: 1 });
    },

    /*
     * 获取学校基本信息
     */
    getSchoolInfo(schoolId) {
        school.getSchoolInfo(schoolId).then((res) => {
            let thisSchoolInfo = res.data;
            let iconId = thisSchoolInfo.iconId;
            thisSchoolInfo.positionList = JSON.parse(thisSchoolInfo.positionList);
            this.setData({
                schoolInfo: thisSchoolInfo
            });
            this.loadLogo(iconId);
        }).catch((err) => {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        });
    },

    /*
     * 查看学校简介信息
     */
    viewIntroduction() {
        let name = this.data.schoolInfo.name;
        let introduction = this.data.schoolInfo.introduction;
        // 将 introduction 转为 dataUrl
        introduction = encodeURIComponent(introduction);
        wx.navigateTo({
            url: `/pages/schoolIntroduction/schoolIntroduction?name=${name}&introduction=${introduction}`,
        });
    },
    /*
     * 收藏院校
     */
    collectSchool() {
        let isCollect = this.data.schoolInfo.isCollect;
        if (isCollect) {
            // 弹出框询问是否要取消收藏
            // 取消收藏
            school.noCollectSchool(this.data.schoolInfo.id).then((res) => {
                let schoolInfo = this.data.schoolInfo;
                schoolInfo.isCollect = false;
                this.setData({
                    schoolInfo
                });
                wx.showToast({
                    title: '取消收藏成功',
                    icon: 'none',
                    duration: 1000
                });
            }).catch((err) => {
                wx.showToast({
                    title: '请检查网络连接',
                    icon: 'none',
                    duration: 1000
                });
            });
        } else {
            school.collectSchool(this.data.schoolInfo.id).then((res) => {
                let schoolInfo = this.data.schoolInfo;
                schoolInfo.isCollect = true;
                this.setData({
                    schoolInfo
                });
                wx.showToast({
                    title: '收藏成功',
                    icon: 'none',
                    duration: 1000
                });
            }).catch((err) => {
                wx.showToast({
                    title: '请检查网络连接',
                    icon: 'none',
                    duration: 1000
                });
            });
        }
    },
    /*
     * 获取院校所有专业信息
     */
    getAllMajor(schoolId) {
        // let schoolId = this.data.schoolInfo.id;
        school.getAllMajor(schoolId).then((res) => {
            let majorList = res.data.majorList;
            let showMajorList = majorList.slice(0, 3);
            this.setData({
                majorList,
                showMajorList
            });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        });
    },
    /*
     * 跳转到全部专业列表页面
     */
    viewAllMajorList() {
        let majorList = this.data.majorList;
        majorList = JSON.stringify(majorList);
        majorList = encodeURIComponent(majorList);
        wx.navigateTo({
            url: `/pages/majorList/majorList?majorList=${majorList}`,
        });
    },
    /*
     * 获取院校所有咨询内容
     */
    getAllzixun(schoolId) {
        school.getAllzixun(schoolId).then((res) => {
            let zixunList = res.data.zixunList;
            let zixunListLen = zixunList.length;
            this.setData({
                zixunList, zixunListLen
            });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        })
    },
    /*
     * 新页面查看院校资讯内容
     */
    viewzixunContent(e) {
        let zixunId = e.currentTarget.dataset.zixunid;
        wx.navigateTo({
            url: `/pages/zixunContent/zixunContent?zixunId=${zixunId}`,
        });
    },
    /*
     * 新页面查看院校资讯列表
     */
    viewSchoolzixunList(e) {
        let schoolId = this.data.schoolInfo.id;
        wx.navigateTo({
            url: `/pages/schoolzixun/schoolzixun?schoolId=${schoolId}`,
        });
    },
    /*
     * 加载院校logo
     */
    async loadLogo(iconId) {
        try {
            let iconUrl = await file.downloadPublicFile(iconId);
            let schoolInfo = this.data.schoolInfo;
            schoolInfo.logoUrl = iconUrl.data.url;
            this.setData({ schoolInfo });
        } catch (err) {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        };
    }
})